package com.usoft.sso.rest.service.impl;

import com.demo.example.rest.api.entity.RespHeader;
import com.demo.example.rest.api.protobuf.GetUserInfoReq;
import com.demo.example.rest.api.protobuf.GetUserInfoResp;
import com.usoft.sso.common.ProtoBufUtil;
import com.usoft.sso.common.RespUtil;
import com.usoft.sso.exception.SsoRuntimeException;
import com.usoft.sso.rest.domain.UserDomain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author: wangcanyi
 * @date: 2018-07-31 17:57
 */
@RestController
public class UserController {
	private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

	@Resource
	private UserDomain userDomain;

	@GetMapping(value = "/api/user/get")
	public String getUserInfo(HttpServletRequest request, GetUserInfoReq.Builder req) {
		GetUserInfoResp.Builder builder = GetUserInfoResp.newBuilder();
		builder.setRespHeader(RespHeader.newBuilder());
		try {
			builder = userDomain.getUserInfo(req.build());
			LOGGER.info("获取用户信息[UserController.getUserInfo]正常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(req.build()));
		} catch (SsoRuntimeException e) {
			LOGGER.warn("获取用户信息[UserController.getUserInfo]告警，参数：{}", ProtoBufUtil.toJSONHasTryCatch(req), e);
			RespHeader respHeader = RespUtil.getRespHeader(e.getErrorCode(), e.getErrorMessage());
			builder.setRespHeader(respHeader);
		} catch (Exception e) {
			LOGGER.error("获取用户信息[UserController.getUserInfo]异常，参数：{}", ProtoBufUtil.toJSONHasTryCatch(req), e);
				RespHeader respHeader = RespUtil.getRespHeader(SsoRuntimeException.ERROR_CODE_100, e.getMessage());
			builder.setRespHeader(respHeader);
		}
		return ProtoBufUtil.toJSONHasTryCatch(builder.build());
	}

}
